const router = require('express').Router()
const mysql = require('./../mysql')
const News = require('./../mysql/collection/News')
const uuid = require('uuid')
/**
 * @api {get} /admin/news/list 新闻列表
 * @apiName AdminNews
 * @apiGroup News
 * 
 * @apiHeader { string } token
 * 
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 *
 * @apiSuccessExample Success-Response:
 *     {
 *       "code": "200",
 *       "message": "新闻列表" 
 *     }
 */
router.get('/list', (req, res, next) => {
  mysql.find(News, {}, { _id: 0, __v: 0 }).then((data) => {
    res.send({
      code: '200',
      message: '产看文章列表',
      data
    })
  })
})
/**
 * @api {post} /admin/news/add 插入新闻
 * @apiName AdminNewsAdd
 * @apiGroup News
 * @apiHeader {String} token  token
 * @apiParam {String} title 新闻标题
 * @apiParam {String} desc 新闻简介
 * @apiParam {String} author 作者
 * @apiParam {String} createTime 发布时间
 * @apiParam {Number} recommend 是否推荐 1推荐 0 不推荐
 * @apiParam {String} content 新闻内容
 * @apiParam {Number} flag 是否可查看 1可以 0不可以
 * @apiParam {String} img 新闻展示图
 * @apiSuccessExample {json} Success-Response:
 *  {
 *    code: '200',
 *    message: '插入新闻'
 *  }
 */
router.post('/add', (req, res, next) => {
  const insertData = req.body
  insertData.id = 'news_' + uuid.v4()
  insertData.recommend = 0
  insertData.flag = 0
  mysql.insert(News, insertData).then(() => {
    res.send({
      code: '200',
      message: '插入新闻成功'
    })
  })
})
/**
 * @api {post} /admin/news/showRecommend 显示推荐新闻
 * @apiName showRecommend
 * @apiGroup News
 * @apiHeader {String} token token
 * 
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiSuccess {String} data  数据
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "显示推荐新闻",
 *       "data": []
 *     }
 */
router.post('/showRecommend', (req, res, next) => {
  mysql.find(News, { recommend: 1 }, { _id: 0, __v: 0}).then(data => {
    res.status(200).send({
      code: '200',
      message: '显示推荐新闻',
      data
    })
  })
})

/**
 * @api {post} /admin/news/showFlag 显示可见新闻
 * @apiName showFlag
 * @apiGroup News
 * @apiHeader {String} token token
 * 
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiSuccess {String} data  数据
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "显示可见新闻",
 *       "data": []
 *     }
 */
router.post('/showFlag', (req, res, next) => {
  mysql.find(News, { flag: 1 }, { _id: 0, __v: 0}).then(data => {
    res.status(200).send({
      code: '200',
      message: '获取数据的依据',
      data
    })
  })
})
module.exports = router